package io.xccit.train.member.controller;

import io.xccit.train.common.page.PageDTO;
import io.xccit.train.common.result.AjaxResult;
import io.xccit.train.member.service.PassengerService;
import io.xccit.train.model.member.domain.dto.PassengerDTO;
import io.xccit.train.model.member.domain.dto.PassengerUpdateDTO;
import io.xccit.train.model.member.domain.vo.PassengerVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author CH_ywx
 * @version 1.0
 * @create 2024/11/12
 * <p>乘客控制器</p>
 */
@RestController
@RequestMapping("/passenger")
public class PassengerController {

    @Autowired
    private PassengerService passengerService;

    @GetMapping("/count")
    public AjaxResult count(){
        return AjaxResult.success(passengerService.count());
    }

    /**
     * 根据id查询乘客信息
     * @param id 乘客ID
     * @return 乘客信息
     */
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        PassengerVO passengerVO = passengerService.selectById(id);
        return AjaxResult.success(passengerVO);
    }

    /**
     * 新增乘客
     * @param passengerDTO 新增乘客信息
     * @return
     */
    @PostMapping
    public AjaxResult insert(@Valid @RequestBody PassengerDTO passengerDTO){
        passengerService.insert(passengerDTO);
        return AjaxResult.success();
    }

    /**
     * 修改乘客信息
     * @param passengerUpdateDTO 修改乘客信息
     * @return
     */
    @PutMapping
    public AjaxResult update(@Valid @RequestBody PassengerUpdateDTO passengerUpdateDTO){
        passengerService.update(passengerUpdateDTO);
        return AjaxResult.success();
    }

    /**
     * 根据ID列表删除乘客
     * @param ids 乘客ID集合
     * @return
     */
    @DeleteMapping
    public AjaxResult delete(@RequestBody Long[] ids){
        passengerService.deleteAndBatch(ids);
        return AjaxResult.success();
    }

    /**
     * 查询乘客列表
     * @return
     */
    @GetMapping
    public AjaxResult listPage(PageDTO pageDTO){
        return AjaxResult.success(passengerService.list(pageDTO));
    }
}
